home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1065 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: b91926@fsgi01.fnal.gov (David Sachs)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Ambiguity when overloading operators
  5. Date: 12 Apr 1996 20:17:15 GMT
  6. Organization: FERMILAB, Batavia, IL
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4kmd34$933@fsgi01.fnal.gov>
  9. References: <960411233436_100754.2730_GHV68-1@CompuServe.COM>
  10. Reply-To: sachs@fnal.fnal.gov
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. X-Newsreader: NN version 6.5.0 #9 (NOV)
  13. Content-Length: 1346
  14. X-Lines: 44
  15. Originator: clamage@taumet
  16.  
  17. Martin Aupperle <100754.2730@CompuServe.COM> writes:
  18.  
  19. >If I define
  20.  
  21. >  struct  X {
  22.  
  23. >    X( int );
  24. >    operator const char* () const;
  25.  
  26. >    /* other members */
  27. >    };
  28.  
  29. >  X& operator + ( const X&, const X& );
  30.  
  31. >I cannot say 
  32.  
  33. >  X x1( 5 );
  34. >  X x2 = x1+3;  // ambiguous
  35.  
  36. >Borland BC4.5 says that x1+3 is ambiguous. I know that it can
  37. >1. convert 3 to an X and call operator + ( const X&, constX& )
  38. >2. convert x1 to a const char* and do pointer arithmetics. 
  39.  
  40. >Is the compiler right? I have in mind that a conversion towards a user defined
  41. >type has precedence over the conversion to a fundamental type. So choice (1)
  42. >should be right and it shoud not be ambiguous. 
  43.  
  44. The compiler is right. The expression x1+3 is treated as if it were
  45. a function call: operator+(x1,3). There are 2 relevant candidates.
  46.  
  47. 1) operator+(const X&, const X&)
  48. 2) operator+(char* const, const int)   (built-in pointer arithmetic)
  49.  
  50. The first form is a better match for the first argument. The second
  51. for is a better match for the second argument. Therefore the construct
  52. is ambiguous.
  53.  
  54. If you had an operator+(const X&, const int), it would be a best match
  55. and would be used.
  56. -- 
  57. ***** The stories about the first lady are hilarious. *****
  58. David Sachs - Fermilab, HPPC MS369 - P. O. Box 500 - Batavia, IL 60510
  59. Voice: 1 708 840 3942      Deparment Fax: 1 708 840 3785
  60.  
  61.  
  62.  
  63. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  64. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  65. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  66. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  67. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  68.